﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_CostRecord")]
    public class T_CostRecord : IEntity<T_CostRecord>
    {
        [Key]
        [Column(TypeName="int")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Column(TypeName="varchar")]
        [StringLength(128)]
        public string UserName { get; set; }
        /// <summary>
        /// 消费金额
        /// </summary>
        public decimal Cost { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string ProductName { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string Remark { get; set; }
        /// <summary>
        /// 消费时间
        /// </summary>
        public DateTime CostDate { get; set; }
        /// <summary>
        /// 消费后余额
        /// </summary>
        public decimal Balance { get; set; }

        public override T_CostRecord Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_CostRecord Find(System.Linq.Expressions.Expression<Func<T_CostRecord, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.CostRecord.FirstOrDefault(where);
                if (item != null)
                {
                    this.Balance = item.Balance;
                    this.Cost = item.Cost;
                    this.CostDate = item.CostDate;
                    this.Id = item.Id;
                    this.ProductName = item.ProductName;
                    this.Remark = item.Remark;
                    this.UserName = item.UserName;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_CostRecord, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.CostRecord.Any(where);
            }
        }

        public override T_CostRecord FindLast(System.Linq.Expressions.Expression<Func<T_CostRecord, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.CostRecord.OrderByDescending(c=>c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.Balance = item.Balance;
                    this.Cost = item.Cost;
                    this.CostDate = item.CostDate;
                    this.Id = item.Id;
                    this.ProductName = item.ProductName;
                    this.Remark = item.Remark;
                    this.UserName = item.UserName;
                    return this;
                }
                return null;
            }
        }
    }
}
